package com.sam.db;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Database {
	static Connection connection = null;

	String driverName = "com.mysql.jdbc.Driver"; // for MySql
	String uri = "jdbc:mysql://localhost:3306/sam"; // for Mysql
	String username = "root"; // You should modify this.
	String password = ""; // You should modify this.
    //INSERT INTO `sam`.`sam_video_log` (`video_id`, `video_name`, `date`) VALUES ('1', 'outputfile', '2013-07-01');
	public Database() {

		System.out.println("in database Constructor");

		try {
			// Load the JDBC driver
			Class.forName(driverName);
			// Create a connection to the database
			System.out.println("for name done");
			connection = (Connection) DriverManager.getConnection(uri,
					username, password);
			System.out.print("connection done");
		} catch (ClassNotFoundException e) {
			// Could not find the database driver

			System.out.println("ClassNotFoundException : " + e.getMessage());

		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println(e.getMessage());

		}

		System.out.println("" + driverName);

		
		
	}
	
	public boolean insertMovie(String[] agv){
		
		String query = "INSERT INTO sam.sam_video_log (video_name,date) VALUES ('"+agv[0]+"','"+agv[1]+"')";
		Statement stmt = null;
	
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	
		
		return true;
	}
	
	public String[] getThreats(){
		
		String query = "select video_name from sam_video_log";
		
		// "where username="+values[0]+"AND password="+value[1]+";

		Statement stmt = null;
		ResultSet rs = null;
		String[] name = null;
		int size=0;
		int i=0;
		try {
			stmt = (Statement) connection.createStatement();
			rs = stmt.executeQuery(query);
			System.out.println("value of = "+rs);
			while(rs.next()){
				size++;
				
			}
			name = new String[size];
			
			rs.beforeFirst();
			
			while (rs.next()) {
				name[i++] = rs.getString(1); // or rs.getString("NAME");
			} 
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
		return name;
		
				
	}

	public boolean setSessionId(String username) {
		
		String query = "update sam_login set session_id="+1235456322+" where username='"+username+"'";

		Statement stmt = null;
		
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}

		return true;

	}
	
public boolean removeSessionId(String username) {
		
		String query = "update sam_login set session_id=none where username='"+username+"'";

		Statement stmt = null;
	
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}

		return true;

	}

    public boolean insertAlert(String[] arg){
    	
    	String query = "INSERT INTO sam.sam_alert_log (alert_name,date,status) VALUES ('"+arg[0]+"','"+arg[1]+"','"+arg[2]+"')";
		Statement stmt = null;
	
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
			return true;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	return false;
		
		
    
    }
    
 
	public String getSessionId(String value) {
		String query = "select session_id from sam_login where username='" + value+"'";
		Statement stmt = null;
		ResultSet rs = null;
		String sessionId = "none";
		

		try {
			stmt = (Statement) connection.createStatement();
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				sessionId = rs.getString(1);
				
			} 
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
			
		return sessionId;
		
		
	}

	public boolean auth(String[] values) {

		String query = "select * from sam_login where username='" + values[0]
				+ "'and password='" + values[1] + "'";

		
		Statement stmt = null;
		ResultSet rs = null;
		username = "none";
		password = "none";

		try {
			stmt = (Statement) connection.createStatement();
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				username = rs.getString(1); // or rs.getString("NAME");
				password = rs.getString(2);
				
			} 
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		if (username.equals("none") && password.equals("none"))
			return false;
		else
			return true;
	}
	
public boolean setAlertStatus(String videoname ,String status){
		
		String query = "update sam_alert_log set status='"+status+"' where video_name='"+videoname+"'";

		Statement stmt = null;
		
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}

		return true;
		
		
		
	}
	
	public boolean setStatus(String videoname ,String status){
		
		String query = "update sam_video_log set status='"+status+"' where video_name='"+videoname+"'";

		Statement stmt = null;
		
		
		try {
			stmt = (Statement) connection.createStatement();
			stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}

		return true;
		
		
		
	}
	
	/*public static void main(String[] argv){
		Database db = new Database();
		String[] a ={"ali","ali"};
		if(db.auth(a));
		else
			System.out.println("not found");
	}*/

}
